package com.aube.plugin.wap;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.share.ShareModule;
import com.huyn.baseframework.share.ShareType;
import com.huyn.baseframework.share.WeiboUtil;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.Utils;

/**
 * Created by huyaonan on 16/9/26.
 */
public class H5ShareHelper {

    private Context context;
    private String mShareWeiboImgPath, mShareWechatImgPath;
    private String title, content, picUrl, url, weiboPic, weiboContent;
    private IShareBitmapListener mListener;

    public H5ShareHelper(Context context, String title, String content, String picUrl, String url, String weiboPic, String weiboContent, IShareBitmapListener listener) {
        this.context = context;
        this.mListener = listener;
        this.title = title;
        this.content = content;
        this.picUrl = picUrl;
        this.url = url;
        this.weiboPic = weiboPic;
        this.weiboContent = weiboContent;

        preloadPics();
    }

    private void preloadPics() {
        if(StringUtils.isNotBlank(weiboPic)) {
            if (StringUtils.isNotBlank(mShareWeiboImgPath))
                return;
            ImageLoader.getInstance(context).preloadBitmapToFile(weiboPic, new ImageLoader.IPreloadCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    if (StringUtils.isNotBlank(s)) {
                        mShareWeiboImgPath = s;
                    }
                }

                @Override
                public void onFail() {

                }
            });
        }
        if(StringUtils.isNotBlank(picUrl)) {
            if (StringUtils.isNotBlank(mShareWechatImgPath))
                return;
            ImageLoader.getInstance(context).preloadBitmapToFile(picUrl, 250, 250, new ImageLoader.IPreloadCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    if (StringUtils.isNotBlank(s)) {
                        mShareWechatImgPath = s;
                    }
                }

                @Override
                public void onFail() {

                }
            });
        }
    }

    public void toShare(final int type) {
        if(shouldRequestPermission()) {
            return;
        }
        if (type == ShareType.WEIBO) {
            if(StringUtils.isNotBlank(mShareWeiboImgPath)) {
                shareImg(Utils.copyToFile(mShareWechatImgPath), weiboContent);
                return;
            }
        } else {
            if(StringUtils.isNotBlank(mShareWechatImgPath)) {
                if (mListener != null)
                    mListener.onResponse(getShareModule(type, Utils.copyToFile(mShareWechatImgPath)));
                return;
            }
        }
        Utils.showToast(context, "分享数据有误");
    }

    /**
     * Checks whether it is necessary to ask for permission to read storage. If necessary, it also
     * requests permission.
     *
     * @return true if a permission request is made. False if it is not necessary.
     */
    @TargetApi(23)
    private boolean shouldRequestPermission() {
        if (requiresPermission()) {
            ((Activity) context).requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
            Utils.showToast(context, "文件文法访问,请先在应用程序设置中打开存储权限");
            return true;
        } else {
            return false;
        }
    }

    @TargetApi(23)
    private boolean requiresPermission() {
        return getSDKINT() >= 23
                && context.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED;
    }

    private int getSDKINT() {
        return (Build.VERSION.SDK_INT == 23 && Build.VERSION.CODENAME.charAt(0) == 'N') ? 24
                        : Build.VERSION.SDK_INT;
    }

    /**
     * 组装分享
     */
    private ShareModule getShareModule(int type, String bitmap) {
        final ShareModule shareModule = new ShareModule();
        shareModule.mShareChannel = type;
        shareModule.shareUrl = url;
        shareModule.title = title;
        shareModule.content = content;
        //图片大小不能超过32kb
        shareModule.imagePath = bitmap;
        return shareModule;
    }

    private void shareImg(String path, String content) {
        try {
            if("ERROR".equalsIgnoreCase(path)) {
                Utils.showToast(context, "文件文法访问,请先在应用程序设置中打开存储权限");
                return;
            }
            ShareModule shareModule = new ShareModule();
            shareModule.imagePath = path;
            shareModule.content = content;
            WeiboUtil.getInstance((Activity) context).startShareImg((Activity) context, shareModule);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface IShareBitmapListener {
        public void onResponse(ShareModule shareModule);
    }


}
